<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" cancelButton="none" :radius="18" :focus="true" />
		</view>
		<view class="sugg-list" v-if="searchResult.length">
			<view class="sugg-item" v-for="(item, index) in searchResult" :key="index" @click="gotoGoodsDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="16" @click="clearKeyword"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, index) in historyList" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				searchResult: [],
				historyList: []
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kws') || '[]')
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.getSearchResult(e)
				}, 500)
			},
			async getSearchResult(query) {
				if(!query.length) {
					this.searchResult = []
					return
				}
				const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', {query})
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message
				this.saveSearchKeyword(query)
			},
			gotoGoodsDetail(goods_id) {
				uni.navigateTo({
					url: `/subpak/good_detail/good_detail?goods_id=${goods_id}`
				})
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: `/subpak/good_list/good_list?query=${item}`
				})
			},
			saveSearchKeyword(value) {
				if(this.historyList.includes((value))) return
				this.historyList.unshift(value)
				uni.setStorageSync('kws', JSON.stringify(this.historyList))
				// const set = new Set(this.historyList)
				// set.delete(value)
				// set.add(value)
				// this.historyList = Array.from(set)
			},
			clearKeyword() {
				this.historyList = []
				uni.setStorageSync('kws', '')
			}
		},
		onUnload() {
			this.timer = null
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
		background-color: #c00000;
		uni-search-bar {
		}
	}
	.sugg-list {
		padding: 0 5px;
		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.goods-name {
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
	.history-box {
		padding: 0 5px;
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}
		.history-list {
			display: flex;
			flex-wrap: wrap;
			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>
